﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace EDMS
{
    public partial class VoterInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Frame.Global.GlobalFunctions.InitComboBoxColumn(gvVoter.Columns["t_xjbh"], "xjdj", "xjmc", "xjbh");
            }

        }

        //删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            LinkButton li = (LinkButton)sender;
            string id = li.CommandArgument;
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                var result = dc.Xm.FirstOrDefault(x => x.sfzh == id);
                if (result != null)
                {
                    dc.Xm.DeleteOnSubmit(result);
                    dc.SubmitChanges();
                    Response.Write("<script>alert('删除成功！');</script>");
                    //location.href='VoterInfo.aspx';
                }
            }
        }

        protected void LDSVoter_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {

        }
        //查询
        protected void CBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            string SqlFormat = @"select xjbh,sfzh,xmxm,cym,sjhm,flag,sybwtcs,xb,sr,zz from xm where {0} 1=1";
            StringBuilder strb = new StringBuilder();
            List<SqlParameter> Params = new List<SqlParameter>();
            if (!string.IsNullOrEmpty(cmbXjbhz.Text.Trim()))
            {
                strb.Append("xjbh=@bh  AND ");
                Params.Add(new SqlParameter("@bh", cmbXjbhz.Value.ToString()));
            }
            if (!string.IsNullOrEmpty(txtXmxm.Text.Trim()))
            {
                strb.Append("xmxm like ''+@xm+'%'  AND ");
                Params.Add(new SqlParameter("@xm", txtXmxm.Text.ToString()));
            }
            if (!string.IsNullOrEmpty(txtSfzh.Text.Trim()))
            {
                strb.Append("sfzh like ''+@sfz+'%'  AND ");
                Params.Add(new SqlParameter("@sfz", txtSfzh.Text.ToString()));
            }
            if (!string.IsNullOrEmpty(txtSjhm.Text.Trim()))
            {
                strb.Append("sjhm like ''+@sjh+'%'  AND ");
                Params.Add(new SqlParameter("@sjh", txtSjhm.Text.ToString()));
            }
            if (!string.IsNullOrEmpty(cmbXb.Text.Trim()))
            {
                strb.Append("xb =@xb  AND ");
                Params.Add(new SqlParameter("@xb", cmbXb.Value.ToString()));
            }
            this.gvVoter.DataSourceID = "";
            gvVoter.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(string.Format(SqlFormat, strb.ToString()), Params.ToArray());
            gvVoter.DataBind();
        }
        //判断身份证号是否重复
        private static bool checkSfzh(string sf)
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                Xm vo = dc.Xm.FirstOrDefault(v => v.sfzh == sf);
                if (vo != null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
        //确认添加
        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (txtXmxmz.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请输入选民姓名！");
            }
            //身份证号重复，不允许录入
            else if (txtSfzhz.Text != null && checkSfzh(txtSfzh.Text) == false)
            {
                Frame.Global.GlobalFunctions.Alert("不允许录入重复的身份证号！");
            }
            //else if (txtSybwtcs.Text.Equals(""))
            //{
            //    Frame.Global.GlobalFunctions.Alert("请填写剩余委托次数！");
            //}
            else if (cmbXbz.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请选择性别！");
            }
            else
            {
                insertVoter();
            }
        }
        //添加选民信息
        private void insertVoter()
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                Xm xm = new Xm();
                xm.xjbh = cmbXjbh.Value.ToString();
                xm.xmxm = txtXmxmz.Text.Trim().ToString();
                //身份证号不重复，用户录入空值，取当前时间为ID
                if (txtSfzh.Text == "" && checkSfzh(txtSfzh.Text) == true)
                {
                    xm.sfzh = DateTime.Now.ToString("yyyyMMddHHmmssff"); ;
                }
                //身份证号不重复，非空录入，取文本框值
                if (txtSfzhz.Text != "" && checkSfzh(txtSfzhz.Text) == true)
                {
                    xm.sfzh = txtSfzhz.Text.Trim().ToString();
                }
                xm.sjhm = txtSjhmz.Text.Trim().ToString();
                xm.flag = "Y";
                xm.sybwtcs = 3;
                xm.xb = cmbXbz.Value.ToString();
                xm.sr = deSr.Text.ToString();
                xm.zz = txtZz.Text;
                xm.cym = txtCym.Text;
                dc.Xm.InsertOnSubmit(xm);
                dc.SubmitChanges();
                Frame.Global.GlobalFunctions.Alert("选民信息添加成功");
            }
        }
        //取消
        protected void btnEsc_Click(object sender, EventArgs e)
        {
            cmbXjbh.Text = "";
            cmbXb.Text = "";
            txtSfzh.Text = "";
            txtSjhm.Text = "";
            //txtSybwtcs.Text = "";
            txtXmxm.Text = "";
            deSr.Text = "";
            txtZz.Text = "";
            txtCym.Text = "";
        }
        //清空检索条件
        protected void btnQk_Click(object sender, EventArgs e)
        {
            cmbXjbhz.Text = "";
            txtXmxm.Text = "";
            txtSfzh.Text = "";
            txtSjhm.Text = "";
            cmbXb.Text = "";
            this.gvVoter.DataSource = "";
            this.gvVoter.DataBind();
        }
        //确认修改
        protected void btnXg_Click(object sender, EventArgs e)
        {
            if (txtXmxmx.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请输入选民姓名！");
            }
            //else if (txtSybwtcs.Text.Equals(""))
            //{
            //    Frame.Global.GlobalFunctions.Alert("请填写剩余委托次数！");
            //}
            else if (cmbXbx.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请选择性别！");
            }
            else
            {
                updateVoter();
            }
        }
        //修改信息
        private void updateVoter()
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                var xm = dc.Xm.Select(x => x)
                   .Where(x => x.sfzh == txtSfzhx.Text.ToString())
                   .SingleOrDefault();
                xm.xmxm = txtXmxmx.Text.Trim().ToString();
                xm.sfzh = txtSfzhx.Text.Trim().ToString();
                xm.sjhm = txtSjhmx.Text.Trim().ToString();
                //xm.sybwtcs = Convert.ToInt32(txtSybwtcsx.Text);
                xm.xb = cmbXbx.Value.ToString();
                xm.sr = deSrx.Text.ToString();
                xm.zz = txtZzx.Text;
                xm.cym = txtCymx.Text;
                dc.SubmitChanges();
                Frame.Global.GlobalFunctions.Alert("选民信息修改成功！");
            };
        }
    }
}